package bookshop.controller.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.CustComment;
import bookshop.domain.Customer;
import bookshop.domain.Manager;
import bookshop.domain.OrderInfo;
import bookshop.domain.OrderItem;
import bookshop.domain.OrderTrace;

import bookshop.service.CustomerService;

import bookshop.service.CustCommentService;

import bookshop.service.OrderService;
import bookshop.service.OrderTraceService;

import bookshop.service.helper.CustomerQueryHelper;

import bookshop.service.helper.CustCommentQueryHelper;

import bookshop.service.helper.OrderQueryHelper;

import bookshop.service.impl.CustomerServiceImpl;

import bookshop.service.impl.CustCommentServiceImpl;

import bookshop.service.impl.OrderServiceImpl;
import bookshop.service.impl.OrderTraceServiceImpl;
import bookshop.utils.Page;

public class MyDDServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String act = request.getParameter("act");
		
		if("showMyDD".equals(act)){
			if(request.getSession().getAttribute("loginedCustomer")!=null){
			request.getRequestDispatcher("/jsps/front/mydd/main_mydd.jsp").forward(request, response);
			}else{
				String err = "请登录！";
				request.getSession().setAttribute("errMsg", err);
				response.sendRedirect("customer?act=toLogin");
			}
		}else if("CustOrder".equals(act)){
			Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");
			OrderQueryHelper helper = new OrderQueryHelper();
			helper.setQryCustId(customer.getCustomerId());
			Page page = new Page();
			page.setPageSize(5);
			if(request.getParameter("pageno")!=null){
            page.setPageNo(Integer.parseInt(request.getParameter("pageno")));			
			}
			
			OrderService orderService = new OrderServiceImpl();
			page = orderService.loadOrdersByCondition(helper, page);
			
			List<OrderItem> orderItems = new ArrayList<OrderItem>();

			for (Object item : page.getPageContent().toArray()) {
				OrderInfo order = (OrderInfo) item;

				for (OrderItem orderItem : orderService
						.GetOrderItemByOrderId(order.getOrderId())) {
					orderItems.add(orderItem);

				}
			}
			request.setAttribute("orderItems", orderItems);
			request.setAttribute("page", page);
			
			CustCommentQueryHelper helpers = new CustCommentQueryHelper();
			helpers.setCustId(customer.getCustomerId());
			Page pages = new Page();
			pages.setPageSize(100);
			CustCommentService commentService = new CustCommentServiceImpl();
			
			request.setAttribute("comments", commentService.loadCommentsByCondition(helpers, pages));
		
			
			
			request.getRequestDispatcher("/jsps/front/mydd/list_order.jsp").forward(request, response);
		}else if("orderProcess".equals(act)){
			String orderId = request.getParameter("orderId");
			String orderStatus = request.getParameter("orderStatus");
			Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");
			OrderService orderService = new OrderServiceImpl();
            OrderInfo order= new OrderInfo();
			order = orderService.GetOrderByOrderId(orderId);
			order.setOrderStatus(orderStatus);
			orderService.UpdateOrder(order);
			
			 Manager manager = new Manager();
			
			OrderTrace orderTrace = new OrderTrace();
			orderTrace.setCustomer(customer);
			orderTrace.setManager(manager);
			orderTrace.setOrderId(orderId);
			orderTrace.setOrderStatus(orderStatus);
			orderTrace.setOperTime(new java.util.Date());
			
			OrderTraceService orderTraceService = new OrderTraceServiceImpl();
			orderTraceService.SaveOrderTrace(orderTrace);
			
			response.sendRedirect("myDD?act=CustOrder");
			
		}
		else if("toupdateCustomer".equals(act)){
			Customer customer =(Customer)request.getSession().getAttribute("loginedCustomer");
			String customerId =customer.getCustomerId();
			request.setAttribute("customerId", customerId);
			request.getRequestDispatcher("jsps/front/mydd/update_personal.jsp").forward(request, response);
		}
		else if("updateCustomer".equals(act)){
			String customerId = request.getParameter("customerid");
			String oldcustomerPwd = request.getParameter("oldcustomerpwd");
			String newcustomerPwd = request.getParameter("newcustomerpwd");
			String customerName = request.getParameter("customername");
			
			CustomerService customerService = new CustomerServiceImpl();
			Customer customer = customerService.getCustomerById(customerId);
			
			String result=null;
			if(oldcustomerPwd.equals(customer.getCustomerPwd())){
				customer.setCustomerPwd(newcustomerPwd);
				customer.setCustomerName(customerName);
				customerService.updateCustomer(customer);
				result="修改成功！";
				
				Customer oldcustomer=(Customer)request.getSession().getAttribute("loginedCustomer");
				oldcustomer.setCustomerPwd(newcustomerPwd);
				oldcustomer.setCustomerName(customerName);
				request.getSession().setAttribute("loginedCustomer", oldcustomer);
				
			}
			
			else{
				
				result="修改失败，请检查原密码！";
			}
			request.setAttribute("result", result);
			
			request.getRequestDispatcher("jsps/front/mydd/update_ok.jsp").forward(request, response);
		}
		else if("CustComment".equals(act)){
			Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");
			CustCommentQueryHelper helper =new CustCommentQueryHelper();
			helper.setCustId(customer.getCustomerId());
			
			Page page = new Page();
			if (request.getParameter("pageNo") != null)
				page.setPageNo(Integer.parseInt(request.getParameter("pageNo")));
			
			CustCommentService commentService = new CustCommentServiceImpl();
			page=commentService.loadCommentsByCustId(helper, page);

			request.setAttribute("page", page);		
			request.getRequestDispatcher("jsps/front/mydd/list_comment.jsp").forward(request, response);		
		}
		else if("removeCustComment".equals(act)){
			int id =  Integer.parseInt(request.getParameter("commentId"));
			
			CustCommentService commentService =  new CustCommentServiceImpl();
			commentService.delCustCommentById(id);
			
			response.sendRedirect("myDD?act=CustComment");
		}


	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
